package com.preparation.controller;

import com.preparation.domain.Post;
import com.preparation.domain.Collection;
import com.preparation.dto.R;
import com.preparation.service.ICollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/collects")
public class CollectionController {

    @Autowired
    private ICollectionService collectionService;


    //收藏 取消收藏
    @PostMapping("/collect")
    public R like(@RequestBody Collection collection){
        Boolean flag = collectionService.collect(collection);
        return flag ? R.success("操作成功") : R.error();
    }


    //根据用户id和帖子id查询用户收藏信息
    @GetMapping("/getByUidPid")
    public R getByUidPid(Collection collection){
        Boolean flag = collectionService.getByUidPid(collection);
        return R.success(flag, flag ? "已收藏" : "未收藏");
    }

    //获取用户收藏的帖子列表
    @GetMapping("/getPostsByUid/{userId}")
    public R getPostsByUid(@PathVariable Integer userId){
        List<Post> data = collectionService.getByUid(userId);
        return R.success(data, "查询成功");
    }
}
